package orders;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import quantity.Quantity;
import quantity.QuantityList;


/**
 * Represents a Purchase Order from an individual publisher.
 * Contains the publisher, the id of this PO, and the quantities
 * in this order. 
 * @author SJP
 *
 */
public class PurchaseOrder implements Serializable{
	
	private static final long serialVersionUID = -2648476987713845965L;
	private Publisher pub;
	private String id;
	private Map<String,QuantityList> order;

	/**
	 * Creates a new PurchaseOrder with the given ID and Publisher
	 * @param ID
	 * @param PUB
	 */
	public PurchaseOrder(String ID, Publisher PUB) {
		id = ID;
		pub = PUB;
		order = new HashMap<String,QuantityList>();
	}
	
	/**
	 * @return the publisher
	 */
	public Publisher getPublisher() {return pub;}
	
	/**
	 * @return the id of this PO
	 */
	public String getID() {return id;}
	
	/**
	 * Get the quantities on order for the given isbn.
	 * @param isbn
	 * @return the quantities associated with this isbn
	 */
	public QuantityList getQuantities(String isbn) {return order.get(isbn);}
	
	/**
	 * Add a new Quantity to this order
	 * @param isbn
	 * @param q
	 * @return the quantity that was added
	 */
	public Quantity addQuantity(String isbn, Quantity q) {
		//check if the isbn has already been referenced in this PO
		if (order.containsKey(isbn))
			order.get(isbn).add(q);
		//create a new QuantityList and add this quantity to it.
		else {
			QuantityList ql = new QuantityList();
			ql.add(q);
			order.put(isbn, ql);
		}
		
		return q;
	}
	
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.defaultWriteObject();
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
	}
}
